import { routerRedux } from 'dva/router'
import { parse } from 'qs'
import config from 'config'
import { EnumRoleType } from 'enums'
import { query, logout } from 'services/app'
import * as menuService from 'services/menu'
import { infoDetais } from 'services/search'
import { type,group } from 'services/newfile'
import { userLis,searchs } from 'services/search'
import { applicationSave1,applicationUpdate1,changeGzcFile1,changeGzcFileSave1,getGzcFileDetails1,getGzcFileList1 } from 'services/fileComponent'

const { prefix } = config

export default {
  namespace: 'fileComponent',
  state: {
	list:'',
	list1:'',
	list2:'',
	params:'',
	showDetails:false,
	details:'',
	menu:0,
	isComponent:"",
	userList:'',
	pagination1: {
      showSizeChanger: true,
      showQuickJumper: true,
      showTotal: total => `共 ${total} 条`,
      current: 1,
      total: null
    },
	pagination2: {
      showSizeChanger: true,
      showQuickJumper: true,
      showTotal: total => `共 ${total} 条`,
      current: 1,
      total: null
	},    
	pagination3: {
		showSizeChanger: true,
		showQuickJumper: true,
		showTotal: total => `共 ${total} 条`,
		current: 1,
		total: null
	  },    
  },
  subscriptions: {
	setup({dispatch,history}){
		const { query,patch } = location
    		history.listen((location)=>{
    			if(location.pathname === '/file/fileComponent'){
    				dispatch({
    					type:'useLists',
    					payload:location.query,
    				})
    				
    			}
    		})
    	}
  },
  effects: {
  	 *applicationSave({payload},{call,put}){
  	 	const data = yield call (applicationSave1,payload)
  	 	yield put({type:'showLoading'})
  	 	if(data.success){
  	 		yield put({ type: 'reload' });
  	 	}else{
  	 		yield put({type:'hideLoading'})
  	 	}
  	 },
  	 *applicationUpdate({payload},{call,put}){
  	 	const data = yield call (applicationUpdate1,payload)
  	 	yield put({type:'showLoading'})
  	 	if(data.success){
  	 		yield put({ type: 'reloads' });
  	 		yield put({
				type:'showDetailSuccess',
				payload:{
					showDetaisl:false
				}
			})
  	 	}else{
  	 		yield put({type:'hideLoading'})
  	 	}
  	 },
  	 *reloads(action, { put, select }) {
	  const page = yield select(({ fileComponent }) => fileComponent.pagination2)
	  const menu = yield select(({ fileComponent }) => fileComponent.menu)
      yield put({ type: 'searchList1', payload: { 
          page:page.current,
          operatingType:0,
          operatingState:1,
       } });
    },
  	 *reload(action, { put, select }) {
	  const page = yield select(({ fileComponent }) => fileComponent.pagination1)
	  const menu = yield select(({ fileComponent }) => fileComponent.menu)
      yield put({ type: 'searchList', payload: { 
          page:page.current,
          operatingState: 0,
		  operatingType: 0
       } });
    },
     *searchList1({payload},{call,put}){
     	const data = yield call (searchs,payload)
     	yield put({
			type:'querySuccess5',
			payload:{
				params:payload,
				list1:data.page.list,
				pagination2:{
					total: data.page.totalCount,
    					current: data.page.currPage
				}
			}
		})
     },
  	 *searchList({payload},{call,put}){
    		const data = yield call (searchs,payload)
		//0为申请 1为审批
    		if(data.success){
    			if(payload.operatingState == 1){
				yield put({
	    				type:'querySuccess5',
	    				payload:{
	    					params:payload,
	    					list1:data.page.list,
	    					pagination2:{
	    						total: data.page.totalCount,
	            				current: data.page.currPage
	    					}
	    				}
	    			})
			}else if(payload.operatingState == 3){
				yield put({
					type:'querySuccess6',
					payload:{
						params:payload,
						list2:data.page.list,
						pagination3:{
							total: data.page.totalCount,
							current: data.page.currPage
						}
					}
				})
			}else{
				yield put({
	    				type:'querySuccess3',
	    				payload:{
	    					params:payload,
	    					list:data.page.list,
	    					pagination1:{
	    						total: data.page.totalCount,
	            				current: data.page.currPage
	    					}
	    				}
	    			})
			}
    			
    		}
    },
	*showDet({payload},{call,put}){
		const data = yield call (infoDetais, payload)
		yield put({
			type:'showDetailSuccess',
			payload:{
				showDetails:true,
				details:data
			}
		})
	},
	*showList({payload},{call,put}){
		yield put({
			type:'showDetailSuccess',
			payload:{
				showDetaisl:false
			}
		})
	},
	*showMenu({payload},{call,put}){
		yield put({
			type:'showMenuSuccess',
			payload:{
				menu:payload
			}
		})
	},
	*isFileComponent({payload},{call,put}){
		yield put({
			type:'showChangeSuccess',
			payload:{
				isComponent:payload
			}
		})
	},
    *useLists({payload},{call,put}){
    		const data = yield call (userLis,payload)
    		if(data.success){
    			yield put({
    				type:'querySuccess2',
    				payload:{
    					userList:data.userList
    				}
    			})
    		}
    },
  },
  reducers: {
	showDetailSuccess(state,action){
		const {showDetails,details} = action.payload
		return {
			...state,
			showDetails,
			details
		}
	},
	showMenuSuccess(state,action){
		const {menu} = action.payload
		return {
			...state,
			menu
		}
	},
	showChangeSuccess(state,action){
		const {isComponent} = action.payload
		return {
			...state,
			isComponent
		}
	},
	querySuccess2(state,action){
    		const {userList} = action.payload
    		return {...state,
    			userList
    		}
    },
   	querySuccess3(state,action){
    		const {list,pagination1,params} = action.payload
    		return {...state,
    			list,
    			params,
    			pagination1: {
	          ...state.pagination1,
	          ...pagination1
	        },
    		}
    },
   	querySuccess5(state,action){
    		const {params,list1,pagination2} = action.payload
    		return {...state,
				list1,
				params,
	        pagination2: {
	          ...state.pagination2,
	          ...pagination2
	        },
    		}
	},   
	querySuccess6 (state,action){
		const {params,list2,pagination3} = action.payload
		return {...state,
			list2,
			params,
			pagination3: {
		  		...state.pagination3,
		  		...pagination3
			},
		}
},  
   	hideLoading (state) {
      return { ...state, loading: false }
    },
    showLoading (state) {
      return { ...state, loading: true }
    },
  },
}
